/**
 * Created by wangxin on 2017-9-26 10:02:01.
 */

'use strict';
const page = {
    url: {
        eventList: contextData.contextPath + '/work/eventList',
    },
    requestFlag: false,
};

page.init = function () {
    page.initElement();
    page.initEvent();
};

page.initElement = function () {
    const thisPage = this;
    Dolphin.ajax({
        url: thisPage.url.eventList,
        type: "get",
        data: {token: contextData.data.gId},
        onSuccess: function (data) {
           var body = $("#body");
           var eventList = data.rows;
            if(eventList.length){
               eventList.forEach(event=>{
                   var alink = $("<a style='text-decoration:none;color: white'></a>");
                   var eventDom = $("<div style='width: 96%;position: relative;padding: 1% 2%'></div>");
                   var img = $("<img style='width: 100%'>")
                   var text = $("<div style='position: absolute;top: 15%;left: 8%;word-wrap: break-word;font-size: 24px;width: 50%'></div>")
                   var endDate = $("<div style='position: absolute;bottom: 15%;left: 8%;font-size: 12px'></div>");
                   var d = event.eventDateEnd && thisPage.formatterDate(event.eventDateEnd, 'yyyy年MM月dd日hh:mm') ||　'';
                   if(d){
                       endDate.text(d+'截止');
                   }else {
                       endDate.text('当前活动无截止时间');
                   }
                   text.text(event.eventName);
                   img.attr('src',event.background);
                   eventDom.append(img).append(text).append(endDate);
                   alink.attr('href',event.link);
                   alink.append(eventDom);
                   body.append(alink);
                   console.log(body);
               })

           }

        },
        onError: function (e) {
            alert('网络出错啦，请您重新加载');
        }
    });
};

page.initEvent = function () {
};

page.formatterDate = function (val, formatter) {
    if(val){
        var date = Dolphin.string2date(val, "yyyy-MM-ddThh:mm:ss.");
        date.setHours(date.getHours() + 8);
        return Dolphin.date2string(date, formatter || "yyyy-MM-dd");
    }else{
        return '';
    }
};

$(function () {
    page.init();
});




